package com.naiyumie.ncalculator;

import java.math.BigDecimal;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * 숫자를 몇 퍼센트 감소.
 */
public class Percent04Activity extends KingWangJjangActivity {
	
	EditText etc1 = null, etc2 = null, etc3 = null;
	BigDecimal myNumeric;
	String myNumericStr = null;
	BigDecimal myPercentage;
	String myPercentageStr = null;
	BigDecimal resultValue4;
	String resultValueStr4 = null;
	String msg = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_percent_04);
		Intent fromIntent = getIntent();
		msg = fromIntent.getStringExtra("MSG");
		etc1 = (EditText) findViewById(R.id.etc1);
		etc2 = (EditText) findViewById(R.id.etc2);
		etc3 = (EditText) findViewById(R.id.etc3);
		etc1.addTextChangedListener(decreaseTargetPercentWatcher);
		etc2.addTextChangedListener(decreaseTargetPercentWatcher);
		this.setMsg(msg);
		this.callMethod();
		tvTitle.setText(R.string.category02);
	}
	
	private final TextWatcher decreaseTargetPercentWatcher = new TextWatcher() {
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			etc3.setText("0");
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		public void afterTextChanged(Editable s) {
			myNumericStr = nWidgetTool.stringFromEditText(etc1);
			myNumeric = nWidgetTool.stringToBigDecimalFromEditText(etc1);
			myPercentageStr = nWidgetTool.stringFromEditText(etc2);
			myPercentage = nWidgetTool.stringToBigDecimalFromEditText(etc2);
			if (!myNumericStr.equals(null) && !myNumericStr.equals("") && !myPercentageStr.equals(null) && !myPercentageStr.equals("")) {
				BigDecimal calculate0 = myPercentage.divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);                                //Log.i("calculate0", calculate0.toString());
				BigDecimal calculate1 = calculate0.subtract(new BigDecimal(1));                                                          //Log.i("calculate1", calculate1.toString());
				BigDecimal calculate2 = calculate1.multiply(myNumeric);                                                                  //Log.i("calculate2", calculate2.toString());
				BigDecimal calculate3 = calculate2.multiply(new BigDecimal(-1));                                                         //Log.i("calculate3", calculate3.toString());
				resultValueStr4 = String.valueOf(calculate3);
				etc3.setText(resultValueStr4);
			}
		}
	};
}
